#!/bin/sh
# prepLibioTests - run this before testing libio
#
# modification history
# --------------------
# 01b,31oct00,sn   archive objects into lib<cpu>vx.a
# 01a,31oct00,sn   wrote
#
# <begin_usage>
# Usage: %prog% -cpu <CPU> -from <build_WIND_BASE> -to <install_WIND_BASE>
#
# This script generates a component needed by libio testing. You must specify
# a CPU (corresponds to the VxWorks $(CPU)$(TOOL): e.g. PPC604gnu); a tree
# containing a compiler/libio build for that CPU (typically a view); and a tree 
# containing the CD installation you wish to test (OK - it doesn't *have* to
# be a CD installation but it should be ;) ).
#
# For example:
# %prog% -cpu PPC604gnu -from /view/wrs.tor3_0.stable/wind/river -to /folk/salim/wind-rc
# <end_usage>

build_dir=host/src/gnu/sun4-solaris2
comp_dir=target/config/comps/vxWorks
lib_dir=target/lib
# We removed the following line:
#		iofflush_u.o
cplus_test_objs="iofflush.o \
		iofgets.o \
		iofopen.o \
		iofprintf.o \
		iofputs.o \
		ioftell.o \
		iofwrite.o \
		iogets.o \
		ioputs.o \
		ioscanf.o \
		iosetbuffer.o \
		iosetvbuf.o \
		iosprintf.o \
		iosscanf.o \
		iovsprintf.o \
		iovsscanf.o"
prog=`echo $0 | sed 's@.*/@@'`

# Print usage and exit

Usage ()
{
    # Extract our essence
    begin_usage="# *<begin_usage>"
    end_usage="# *<end_usage>"
    sed -n "/${begin_usage}/,/${end_usage}/{
            /${begin_usage}/d
            /${end_usage}/d
            s/%prog%/$prog/g
            s/^# *//p
            }" $0 
    exit 1
}


# In verbose mode print a message

Message ()
{
    if [ -n "$verbose_f" ]; then
        echo "$*"
    fi
}

# Print error to stderr and exit

Error ()
{
    echo "$prog: Error: $*"
    exit 1
}

# Print warning and return

Warning ()
{
    echo "$prog: Warning: $*"
}


###############################################################################
#
# Main - main routine
#

# Parse command line args

# Verbose by default!
verbose_f=t
cpu=
build_wb=
install_wb=

while [ -n "`echo $1 | grep '-'`" ]; do
    case "$1" in
        # TODO: process command line options
        -v* ) verbose_f=t ;;
        -c* ) shift; cpu=$1 ;;
        -f* ) shift; build_wb=$1 ;;
        -t* ) shift; install_wb=$1 ;;
          * ) Usage ;;
    esac
    shift
done

(test -n "$cpu" && test -n "$build_wb" && test -n "$install_wb") || \
    Usage

# Sanity check WIND_BASE(s)
test -d $build_wb/$build_dir || \
    Error "Can't find $build_wb/$build_dir (maybe you need to start a view?)"

test -d $install_wb/$config_dir || \
    Error "Can't find $install_wb/$config_dir (maybe you need to start a view?)"

# Path to libio.a
libio=`echo $build_wb/$build_dir/*/*/$cpu/libio/libio.a 2>/dev/null | head -1`

test -f "$libio" || \
    Error "Can't find $cpu/libio/libio.a under $build_wb/$build_dir (maybe you need to build it?)"

# Object directory
obj_dir=$install_wb/$lib_dir/obj${cpu}vx
test -d "$obj_dir" || \
    Error "Can't find $obj_dir (maybe you spelled $cpu wrong?)"

vxlib=$install_wb/$lib_dir/lib${cpu}vx.a

test -f "$vxlib" || \
    Error "Can't find $vxlib (maybe you need to build the target side/use a CD installation?)"

# ar from cpu
h_make=$install_wb/target/h/make/make.$cpu
test -f "$h_make" || \
    Error "Can't find $h_make"

ar=ar`grep "TOOLENV.*=" $h_make | sed 's@.*TOOLENV.*=[^a-zA-Z]*\([a-z0-9]*\).*$@\1@'`

$ar -V >/dev/null 2>&1 || \
    Error "Can't run $ar"

# OK, ready to rock

# Install INCLUDE_CPLUS_IOSTREAMS_TEST component

comp=$install_wb/$comp_dir/00comp_cplus_iostreams_test.cdf

Message "Writing $comp ..."

cat > $comp <<EOF
/* 00comp_cplus_iostreams_test.cdf - Component configuration file */

/* Copyright 1984-1999 Wind River Systems, Inc. */

/*
Written automagically by PrepLibioTests on `date`
*/

Component INCLUDE_CPLUS_IOSTREAMS_TEST {
	NAME		C++ iostreams test
	ENTRY_POINTS	ALL_GLOBAL_SYMBOLS
	SYNOPSIS	stdio like functions used for testing iostreams
	MODULES		${cplus_test_objs}
	REQUIRES	INCLUDE_CPLUS 
}
EOF

Message "Extracting objects from $libio to $obj_dir"

cd $obj_dir
for obj in $cplus_test_objs; do
    Message "    $obj"
    $ar x $libio $obj
done

Message "Archiving new objects from $obj_dir to $vxlib"

$ar crus $vxlib $cplus_test_objs
